//
//  YXMineNewsAddressView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/25.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMineNewsAddressView.h"
#import "YXAddressModel.h"
@implementation YXMineNewsAddressView

- (void)setTitle:(NSString *)title {
    _title = title;
    if ([_title isEqualToString:@"修改收货地址"]) {
        [_addBtn setTitle:@"修改地址" forState:(UIControlStateNormal)];
    }else {
        [_addBtn setTitle:@"新增地址" forState:(UIControlStateNormal)];

    }
}

- (void)setModel:(YXAddressModel *)model {
    _model = model;
    _consigneeTF.text = _model.addresseeName;
    _phoneTF.text = _model.phone;
    _addressTF.text = _model.detailedAddress;
    
    if ([_model.defaults isEqualToString:@"1"]) {
        _defaultBtn.selected = YES;
    }else {
        _defaultBtn.selected = NO;
    }
    if ([_title isEqualToString:@"修改收货地址"]) {
         _addressLab.text = [NSString stringWithFormat:@"%@%@%@",_model.provinceName,_model.cityName,_model.areaName];
    }else {
        _addressLab.text = @"";
    }
   
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXMineNewsAddressView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
        self.backgroundColor = color_backColor;
        self.selectAddressView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
        [self.selectAddressView addGestureRecognizer:tap];
    }
    return self;
    
}

- (void)click {
    
    if (self.selectAddressBlock) {
        self.selectAddressBlock();
    }
}

- (IBAction)defaultBtnAction:(UIButton *)sender {
    sender.selected = ! sender.selected;
    if (sender.selected) {
        self.isChoice = @"0";
    }else {
        self.isChoice = @"1";
    }
}


- (IBAction)addressBtnAction:(UIButton *)sender {
    
    if (self.phoneTF.text.length == 0 || self.consigneeTF.text.length == 0 ||self.addressTF.text.length == 0 || self.addressLab.text == nil) {
        [self showTostMessage:@"请填写完整信息！"];
        
    }else {
        if (self.addAddressBlock) {
            self.addAddressBlock(self.isChoice,self.title);
        }
    }

   
}


@end
